#include <iostream>
#define MaxSize 200 //循环队列最大长度
using namespace std;
/* 实现循环队列
 */
typedef int elem; //数据类型
typedef struct QNode
{
    elem data;
    struct QNode *next;
} * QueuePtr;
typedef struct
{
    QueuePtr rear; //队尾
} LinkQueue;
bool init(LinkQueue &L)
{
    L.rear->next = L.rear = new QNode; //头=尾
    return true;
}
//置空序列
bool initQueue(LinkQueue &l)
{
    l.rear = l.rear->next;
    QueuePtr temp;
    while (l.rear != l.rear->next)
    {
        temp = l.rear->next;
        l.rear->next = temp->next;
        delete temp;
    }
    return true;
}
bool isEmpty(LinkQueue l)
{
    return l.rear->next->next == l.rear->next;
    //判断头结点的后一个结点==头结点
}
bool push(LinkQueue &l, elem e)
{
    QueuePtr temp = new QNode;
    temp->data = e;
    temp->next = l.rear->next;
    l.rear->next = temp;
    l.rear = temp;
    return true;
}
bool dequeue(LinkQueue &l, elem &e)
{
    if (isEmpty(l))
        return false;
    QueuePtr temp = l.rear->next->next; //头结点后的第一个元素结点"首元结点"
    e = temp->data;
    if (temp == l.rear)
    {                              //队尾只剩下1个点
        l.rear = l.rear->next;     //尾指针指向头结点
        l.rear->next = temp->next; //头指向头
    }
    else
        l.rear->next->next = temp->next;
    //头的后一个指针指向原来的第二个结点的位置
    delete temp;
    return true;
}
bool show(LinkQueue l)
{
    elem e;
    if (isEmpty(l))
        return false;
    cout << "ShowTime" << endl;
    QueuePtr temp = l.rear->next->next; //头结点后的第一个元素结点"首元结点"
    while (temp != l.rear->next)
    {
        e = temp->data;
        cout << e << " ";
        temp = temp->next;
    }
    return true;
}
int main(int argc, char const *argv[])
{
    LinkQueue S;
    elem e;
    if (init(S))
        cout << "init Succeed" << endl;
    else
        cout << " init Failed" << endl;
    //入队
    cout << "input e:\t";
    while (cin >> e)
    { //按"ctrl+z"结束输入
        push(S, e);
        cout << "input e:\t";
    }
    show(S);
    //出队
    if (dequeue(S, e))
        cout << "移除的头元素是" << e << endl;
    else
        cout << "空" << endl;
    cout << "剩下所有元素为" << endl;
    show(S);
    initQueue(S);
    cout << "置空，栈剩下所有元素为" << endl;
    show(S);
}
